<template>
  <div ref="page">
    <div class="head">
      <div>
        <div>创建人：{{ detailData.createBy }}</div>
        <div>制单时间：{{ detailData.createTime }}</div>
        <div>关联编码：{{ detailData.checkCode }}</div>
        <div>单据编码：{{ detailData.inventoryDealCode }}</div>
      </div>
      <!-- <el-button type="info" text bg @click="$router.back()">返回</el-button> -->
    </div>
    <div class="content">
      <div class="table" :style="{ '--width': width + 'px' }">
        <el-container class="content">
          <el-aside>
            <div class="title">
              仓库：<span>{{ detailData.warehouseName }}</span>
            </div>

            <!-- 商品列表 -->
            <div class="products-list" style="overflow-x: auto">
              <div class="my-table" style="min-width: 1500px">
                <div class="table-header">
                  <div>
                    <div>
                      <div class="col1">商品信息</div>
                      <div class="col3">关联客户</div>
                      <div class="col4">单位</div>
                      <div class="col7">仓库</div>
                      <div class="col5">账面库存数量</div>
                      <div class="col6">账面库存件数</div>
                      <div class="col5">采购金额</div>
                      <div class="col5">报盈数量</div>
                      <div class="col6">报盈件数</div>
                      <div class="col6">涉及采购金额</div>
                    </div>
                  </div>
                </div>

                <div class="table-body" v-if="detailData.list?.length > 0">
                  <div class="list-item" v-for="(item, index) in detailData.list" :key="index">
                    <p class="name">关联客户：{{ item.customerName || "-" }}</p>
                    <div>
                      <div class="col1">
                        <div>
                          <div class="left">
                            <img :src="item.mainPicUrl" alt="" />
                          </div>
                          <div class="right">
                            <h3 class="name">{{ item.productAliasName || "-" }}</h3>
                            <p style="white-space: break-spaces; word-break: break-all">{{ item.skuSpecification }}</p>
                            <p>SKU编码：{{ item.skuCode || "-" }}</p>
                            <p>ID：{{ item.productId || "-" }}</p>
                          </div>
                        </div>
                      </div>
                      <div class="col3" style="white-space: break-spaces; word-break: break-all">
                        <!-- {{ item.customer ? item.customer.split(",")[0] + " " + item.customer.split(",")[1] : "-" }} -->
                        {{ item.customerName || "-" }}{{ item.customerMobileCode }}
                      </div>
                      <div class="col4">
                        {{ paramsOption.shop_unit?.filter(v => v.dictValue == item.productUnit)[0].dictLabel }}
                      </div>
                      <div class="col7">{{ item.warehouseName || "-" }}</div>
                      <div class="col5">
                        {{ item.curProductNum || "-" }}
                      </div>
                      <div class="col6">
                        {{ item.curProductPartNum || "-" }}
                      </div>

                      <div class="col5">
                        {{ item.buyMoney || "-" }}
                      </div>

                      <div class="col5">
                        {{ item.dispProductNum || "-" }}
                      </div>
                      <div class="col6">
                        {{ item.dispProductPartNum || "-" }}
                      </div>

                      <div class="col6">
                        {{ item.dispBuyMoney || "-" }}
                      </div>
                    </div>
                  </div>
                </div>
                <div v-else style="display: flex; justify-content: center; width: 100%">
                  <el-empty description="暂无数据" />
                </div>
              </div>
            </div>
          </el-aside>
          <el-main width="300px">
            <div class="file">
              附件
              <div class="list">
                <div class="code" v-for="(file, index) in detailData.fileUrls" :key="file">
                  <img :src="getIcon(file)" @click="download(file)" />
                  <div class="del" v-if="detailData.dealState == 2" @click.stop>
                    <el-icon @click="deleteFile(index)">
                      <DeleteFilled />
                    </el-icon>
                  </div>
                </div>
                <div v-if="!detailData.fileUrls.length && detailData.dealState == 3">无</div>
                <el-upload
                  v-if="detailData.fileUrls.length < 10 && detailData.dealState == 2"
                  class="avatar-uploader"
                  multiple
                  :show-file-list="false"
                  :http-request="customRequest"
                >
                  <el-icon class="upload">
                    <Plus />
                    <span>点击上传</span>
                  </el-icon>
                </el-upload>
              </div>
              <div class="tip" v-if="detailData.dealState == 2">支持扩展名：doc/docx/pdf/jpg/png，最多上传10个</div>
              <div class="order-remark">订单备注</div>
              <el-form :model="form" v-if="detailData.dealState == 2" :rules="rules" ref="formRef">
                <el-form-item prop="remark">
                  <!-- <el-input type="textarea" placeholder="请输入" v-model="form.remark" /> -->
                  <RemarkEditor v-model="form.remark" />
                </el-form-item>
              </el-form>
              <div class="remark" v-else><RemarkEditor v-model="detailData.remark" :isEdit="false" /></div>
            </div>
          </el-main>
        </el-container>
        <!-- <el-scrollbar height="50px" @scroll="scroll" style="margin: 10px 0" ref="row">
          <div class="table-head">
            <div>商品信息</div>
            <div>关联客户</div>
            <div>单位</div>
            <div>仓库</div>
            <div>账面库存数</div>
            <div>账面库存件数</div>
            <div>采购金额</div>
            <div>报盈数量</div>
            <div>报盈件数</div>
            <div>涉及采购金额</div>
          </div>
        </el-scrollbar>
        <el-scrollbar max-height="calc(100vh - 334px)" @scroll="scroll" ref="tableRow">
          <div class="table-body">
            <div class="table-body-item" v-for="item in detailData.list" :key="item.checkDetailId">
              <div class="name">关联客户：{{ item.customerName }}</div>
              <div class="table-body-item-content">
                <div class="img">
                  <img :src="item.mainPicUrl || defaultImg" alt="" />
                  <div>
                    <div class="goods-name">
                      {{ item.productAliasName }}
                    </div>
                    <div>{{ item.skuSpecification }}</div>
                    <div>SKU编码：{{ item.skuCode }}</div>
                    <div>ID：{{ item.productId }}</div>
                  </div>
                </div>
                <div>{{ item.customerName }}{{ item.customerMobileCode }}</div>
                <div>{{ paramsOption.shop_unit?.filter(v => v.dictValue == item.productUnit)[0].dictLabel }}</div>
                <div>{{ item.warehouseName }}</div>
                <div>{{ item.curProductNum }}</div>
                <div>{{ item.curProductPartNum }}</div>
                <div>{{ item.buyMoney }}</div>
                <div>{{ item.dispProductNum }}</div>
                <div>{{ item.dispProductPartNum }}</div>
                <div>{{ item.dispBuyMoney }}</div>
              </div>
            </div>
          </div>
        </el-scrollbar> -->
      </div>
    </div>
    <div class="confirm" v-if="detailData.dealState == 2">
      <!-- <el-button type="info" text bg @click="$router.back()">取消</el-button> -->
      <el-button type="primary" @click="handOk">提交报告</el-button>
    </div>
  </div>
</template>

<script setup>
import { detail, confirm } from "@/apis/profit";
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import word from "@/assets/images/word.png";
import pdf from "@/assets/images/pdf.png";
import video from "@/assets/images/video.png";
import excel from "@/assets/images/excel.png";
import { useRoute } from "vue-router";
import { selectDictDataByType } from "@/apis/dict/dict";
import upload from "@/utils/upload.js";
import { useElementSize } from "@vueuse/core";
import RemarkEditor from "@/components/RemarkEditor";

const page = ref(null);
const row = ref(null);
const tableRow = ref(null);
const { width } = useElementSize(page);
const route = useRoute();
const formRef = ref(null);
const detailData = ref({
  fileUrls: []
});
const form = ref({
  fileIds: []
});
const init = async () => {
  const res = await detail(route.query.inventoryDealId);
  detailData.value = res.data;
};
const scroll = ({ scrollLeft }) => {
  row.value.scrollTo({
    left: scrollLeft
  });
  tableRow.value.scrollTo({
    left: scrollLeft
  });
};
const rules = {
  remark: [{ max: 200, message: "订单备注不能超过200个字符", trigger: "blur" }]
};
/**
 * 根据类型返回相应图标
 */
const getIcon = url => {
  let icon = "";
  if (getType(url) == "image") icon = url;
  else icon = icons[getType(url)];
  return icon;
};

const fileTypes = {
  image: ["jpg", "png", "jpeg", "gif"],
  video: ["mp4", "avi"],
  excel: ["xls", "xlsx"],
  word: ["doc", "docx"],
  pdf: ["pdf"]
};
const icons = { word, pdf, excel, video };
/**
 * 获取文件类型
 */
const getType = url => {
  let type = "";
  for (let key in fileTypes) {
    fileTypes[key]?.filter(v => {
      if (url.indexOf(v) != -1) {
        type = key;
      }
    });
  }
  return type;
};

//拼接商品的sku规格
const getSpes = properties => {
  if (!properties) return "-";
  const arr = JSON.parse(properties) || [];
  if (arr.length > 1) {
    const str = arr.reduce((a, b) => {
      return a.specificationVal + "+" + b.specificationVal;
    });
    return str;
  } else if (arr.length === 1) {
    return arr[0].specificationVal;
  } else {
    return "-";
  }
};
const download = url => {
  if (detailData.value.dealState == 3) {
    const link = document.createElement("a");
    link.href = url;
    link.click();
  }
};
const isFileMax = ref(false);
const customRequest = async options => {
  const type = [
    "image/jpeg",
    "image/png",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "application/msword",
    "application/pdf",
    "doc"
  ];
  const file = await upload(options, type);
  if (form.value.fileIds.length < 10) {
    form.value.fileIds.push(file.uid);
    detailData.value.fileUrls.push(file.url);
  } else {
    if (!isFileMax.value) ElMessage.error("最多上传10个文件");
    isFileMax.value = true;
  }
};
const deleteFile = index => {
  detailData.value.fileUrls.splice(index, 1);
  form.value.fileIds.splice(index, 1);
};
const handOk = async () => {
  const res = await confirm({
    inventoryDealId: route.query.inventoryDealId,
    fileIds: form.value.fileIds ? form.value.fileIds.join(",") : "",
    remark: form.value.remark
  });
  if (res.code == 200) {
    init();
    ElMessage.success("确认成功");
  }
};
const paramsOption = ref({});
/**
 * 获取列表选项字典
 * @param {*} dictType 字典类型
 */
const getDict = async dictType => {
  const res = await selectDictDataByType(dictType);
  paramsOption.value[dictType] = res.data;
};
onMounted(() => {
  const dictArr = ["shop_pl_type", "shop_deal_state", "shop_unit"];
  dictArr.forEach(v => {
    getDict(v);
  });
  init();
});
</script>

<style lang="scss" scoped>
$border: 1px solid #dcdfe6;
$topic: #007dff;

.head {
  display: flex;
  justify-content: space-between;
  border-bottom: $border;
  height: 62px;
  align-items: center;
  padding: 0 20px;

  > div:first-child {
    display: flex;
    gap: 30px;
  }

  .el-button {
    border-radius: 0;
  }
}

.title {
  color: #86909c;
  font-size: 18px;
  // width: 100%;
  padding-bottom: 20px;

  span {
    color: black;
  }
}
.content {
  height: calc(100vh - 197px);
  :deep(.el-aside) {
    flex: 1;
    padding: 20px;
    header {
      display: flex;
      justify-content: space-between;
      margin-bottom: 18px;
      .supper {
        flex: 1;
        display: flex;
        align-items: center;
        padding-right: 20px;
        p {
          width: 65px;
        }
        .el-select {
          flex: 1;
        }
      }
    }
    .count {
      display: flex;
      justify-content: space-between;
      padding: 10px 20px;
      background-color: #f6f8fa;
      margin-top: 20px;
      margin-bottom: 20px;
      h3 {
        font-size: 16px;
        font-weight: normal;
      }
    }
    .products-list {
      .table-header {
        background-color: #f2f6fc;
        margin-bottom: 10px;
        & > div {
          align-items: center;
        }
      }
      .table-body {
        .list-item {
          margin-bottom: 10px;
          p.name {
            height: 40px;
            background: #f2f6fc;
            line-height: 40px;
            padding-left: 20px;
            border: 1px solid #e1e6ed;
            border-bottom: none;
          }
          & > div {
            border: 1px solid #e1e6ed;
            display: flex;
            & > div {
              border-right: 1px solid #e1e6ed;
              display: flex;
              align-items: center;
            }
            &:not(:first-of-type) {
              border-left: none;
            }
          }
          .col1 {
            & > div {
              display: flex;
            }

            .left {
              img {
                width: 80px;
                height: 80px;
                border-radius: 4px;
              }
            }
            .right {
              padding-left: 18px;
              .name {
                font-size: 14px;
                color: #3d3d3d;
                margin-bottom: 5px;
              }
              p {
                font-size: 12px;
                color: #86909c;
                margin-bottom: 5px;
              }
            }
          }
          .tips {
            display: flex;
            padding: 16px 14px;
            border-top: none;
            border-left: 1px solid #e1e6ed !important;
            p.label {
              width: 38px;
            }
            .el-input {
              flex: 1;
              .el-input__wrapper {
                background-color: #f2f3f5;
              }
            }
          }
        }
      }
      .table-header,
      .table-body {
        & > div > div:first-of-type {
          display: flex;
          justify-content: space-between;
          & > div {
            padding: 14px 20px;
            &.col1 {
              // width: 45%;
              flex: 1;
              min-width: 400px;
            }
            &.col4,
            &.col8 {
              width: 80px;
              box-sizing: border-box;
            }
            &.col3 {
              width: 80px;
            }
            &.col7 {
              width: 100px;
              box-sizing: border-box;
            }
            &.col5,
            &.col2,
            &.col6 {
              width: 125px;
              box-sizing: border-box;
            }
            &.remark {
              width: 224px;
              box-sizing: border-box;
            }
          }
        }
      }
    }
  }
  :deep(.el-main) {
    width: 300px;
    flex: none;
    background-color: #f6f8fa;
    border-left: 1px solid #e2e8f0;
    .pd-info,
    .ps-people {
      p {
        width: 120px;
        margin-bottom: 10px;
      }
      .el-select {
        flex: 1;
      }
    }
    .ps-people {
      & > div {
        display: flex;
        .el-button {
          font-size: 12px;
        }
      }
    }
    .kh-info {
      h3 {
        color: #86909c;
        font-weight: normal;
        margin: 20px 0 16px 0;
      }
      p {
        color: #86909c;
        margin-bottom: 10px;
      }
      b {
        font-weight: normal;
        color: #1a1a1a;
      }
    }
    .sh-info {
      & > p {
        font-size: 16px;
        color: #86909c;
        margin-bottom: 10px;
      }
      .el-form-item {
        flex-direction: column;
      }
      .el-form-item__label-wrap {
        margin-left: 0 !important;
      }
    }
    .ck-tips,
    .file {
      p.label {
        color: #4e5969;
        margin-bottom: 10px;
      }
    }

    .el-upload.el-upload--text {
      width: 80px;
      height: 80px;
      background-color: white;
      border-radius: 4px;
      .el-button {
        width: 100%;
        height: 100%;
      }
    }
    .el-upload-list__item.is-ready {
      width: auto;
      height: auto;
    }
  }
}

// .content {
//   display: flex;

//   > div:last-child {
//     min-width: 300px;
//     background: #f6f8fa;
//     padding: 20px;
//     box-sizing: border-box;
//     height: calc(100vh - 197px);
//   }

//   .table {
//     padding: 20px;
//     box-sizing: border-box;
//     width: calc(var(--width) - 300px);
//     height: calc(100vh - 197px);

//     .title {
//       color: #86909c;
//       font-size: 18px;
//       width: 100%;

//       span {
//         color: black;
//       }
//     }

//     .table-head {
//       height: 50px;
//       line-height: 48px;
//       display: flex;
//       box-sizing: border-box;

//       > div {
//         background: #f6f8fa;
//         text-align: center;
//         width: 120px;
//         min-width: 120px;
//         max-width: 120px;
//         border-top: $border;
//         border-bottom: $border;
//       }

//       > div:first-child {
//         text-align: left;
//         padding-left: 20px;
//         box-sizing: border-box;
//         width: 400px;
//         min-width: 400px;
//         max-width: 400px;
//       }

//       > div:nth-child(2) {
//         width: 200px;
//         min-width: 200px;
//         max-width: 200px;
//       }

//       > div:nth-child(3) {
//         width: 50px;
//         min-width: 50px;
//         max-width: 50px;
//       }
//     }

//     .table-body-item {
//       margin-top: 10px;
//     }

//     .table-body-item:first-child {
//       margin: 0;
//     }

//     .name {
//       height: 40px;
//       background: #f2f6fc;
//       line-height: 40px;
//       padding-left: 20px;
//       border-top: $border;
//       border-bottom: $border;
//       display: flex;
//       width: calc(400px + 200px + 30px + 7 * 120px);
//     }

//     .table-body-item-content {
//       display: flex;

//       > div {
//         text-align: center;
//         width: 100%;
//         padding: 20px 0;
//         border-right: $border;
//         border-bottom: $border;
//         box-sizing: border-box;
//         display: flex;
//         align-items: center;
//         justify-content: center;
//         width: 120px;
//         min-width: 120px;
//         max-width: 120px;
//       }

//       > div:first-child {
//         text-align: left;
//         padding-left: 20px;
//         justify-content: start;
//         width: 400px;
//         min-width: 400px;
//         max-width: 400px;
//       }

//       > div:nth-child(2) {
//         width: 200px;
//         min-width: 200px;
//         max-width: 200px;
//       }

//       > div:nth-child(3) {
//         width: 50px;
//         min-width: 50px;
//         max-width: 50px;
//       }

//       > div:last-child {
//         border-right: none;
//       }

//       .img {
//         display: flex;
//         gap: 20px;

//         img {
//           width: 80px;
//           height: 80px;
//         }

//         > div {
//           color: #86909c;
//           font-size: 12px;
//           line-height: 1.8;
//         }

//         .goods-name {
//           color: #3d3d3d;
//         }
//       }
//     }
//   }
// }

.list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: auto;
  gap: 10px;
  margin: 10px 0;

  .code {
    width: 80px;
    height: 80px;
    background: #f2f3f5;
    position: relative;

    &:hover {
      .del {
        display: flex;
      }
    }

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .del {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgba(0, 0, 0, 0.2);
      display: none;
      color: #f2f3f5;
      font-size: 20px;
    }
  }

  .avatar-uploader {
    .upload {
      width: 80px;
      height: 80px;
      background: #f2f3f5;
      display: grid;
      grid-template-columns: 1fr;
      place-items: center;
      align-self: center;
      color: #c9cdd4;

      span {
        font-style: normal;
      }
    }

    .el-icon svg {
      font-size: 24px;
    }
  }
}

.tip {
  color: #86909c;
  font-size: 11px;
}

.order-remark {
  margin: 10px 0;
}

.el-scrollbar {
  height: unset !important;
  width: unset !important;
  border-left: $border;
  border-right: $border;
}

.el-scrollbar__wrap {
  height: unset !important;
  width: unset !important;
}

.confirm {
  height: 64px;
  background: #ffffff;
  box-shadow: 0px -3px 10px 0px rgba(4, 24, 48, 0.1);
  position: fixed;
  right: 0;
  width: 100vw;
  bottom: 0;
  display: flex;
  justify-content: end;
  align-items: center;
  gap: 10px;
  padding-right: 20px;

  .el-button--primary {
    background: #007dff;
    border-radius: 0;
    border: none;
  }

  .el-button {
    border-radius: 0;
  }
}

.remark {
  color: #1a1a1a;
  word-wrap: break-word;
}
</style>
